from django.core.exceptions import ValidationError
from decimal import Decimal

def validate_order_items(items_data):
    """Validate order items data."""
    if not any(
        key.startswith('item_') and value and int(value) > 0
        for key, value in items_data.items()
    ):
        raise ValidationError('订单必须包含至少一个商品')

def validate_price(price):
    """Validate price is positive."""
    if not isinstance(price, Decimal) or price <= 0:
        raise ValidationError('价格必须为正数')

def validate_quantity(quantity):
    """Validate quantity is positive."""
    try:
        quantity = int(quantity)
        if quantity <= 0:
            raise ValidationError('数量必须为正数')
    except (TypeError, ValueError):
        raise ValidationError('数量必须为有效数字')